{% extends "layout.html" %}
{% block title %}Job Private Instance Manager{% endblock %}
{% block content %}

<h1>{{ jpim.name }}</h1>

<h2>Configuration</h2>
<div class="attributes">
  <form action="{{ base_path }}/config-update/jpim" method="POST">
    <div>Boot disk size (in GB): <input name="boot_disk_size_gb" value="{{ jpim.boot_disk_size_gb }}" /></div>
    <div>Max instances: <input name="max_instances" value="{{ jpim.max_instances }}" /></div>
    <div>Max live instances: <input name="max_live_instances" value="{{ jpim.max_live_instances }}" /></div>
    <div>Max new instances per autoscaler loop: <input name="max_new_instances_per_autoscaler_loop" value="{{ jpim.max_new_instances_per_autoscaler_loop }}" /></div>
    <div>Autoscaler loop period in seconds: <input name="autoscaler_loop_period_secs" value="{{ jpim.autoscaler_loop_period_secs }}" /></div>
    <div>Worker max idle time in seconds: <input name="worker_max_idle_time_secs" value="{{ jpim.worker_max_idle_time_secs }}" /></div>
    <input type="hidden" name="_csrf" value="{{ csrf_token }}"/>
    <button>
      Update
    </button>
  </form>
</div>

<h2>User Resources</h2>
<table class="data-table" id="user_resources">
  <thead>
  <tr>
    <th>User</th>
    <th>Ready Jobs</th>
    <th>Allocated Jobs</th>
    <th>Creating Jobs</th>
    <th>Running Jobs</th>
  </tr>
  </thead>
  <tbody>
  {% for user in user_resources %}
  <tr>
    <td>{{ user['user'] }}</td>
    <td class="numeric-cell">{{ user['n_ready_jobs'] }}</td>
    <td class="numeric-cell">{{ user['n_allocated_jobs'] }}</td>
    <td class="numeric-cell">{{ user['n_creating_jobs'] }}</td>
    <td class="numeric-cell">{{ user['n_running_jobs'] }}</td>
  </tr>
  {% endfor %}
  </tbody>
</table>

<h2>Status</h2>
<div class="attributes">
  <div>Ready jobs: {{ n_ready_jobs }}</div>
  <div>Creating jobs: {{ n_creating_jobs }}</div>
  <div>Running jobs: {{ n_running_jobs }}</div>
</div>
<table class="data-table" id="status">
  <thead>
    <tr>
      <th colspan="4">Instances</th>
      <th></th>
      <th colspan="4">Cores</th>
    </tr>
    <tr>
      <th>Pending</th>
      <th>Active</th>
      <th>Inactive</th>
      <th>Deleted</th>
      <th></th>
      <th>Pending</th>
      <th>Active</th>
      <th>Inactive</th>
      <th>Deleted</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="numeric-cell">{{ jpim.all_versions_instances_by_state['pending'] }}</td>
      <td class="numeric-cell">{{ jpim.all_versions_instances_by_state['active'] }}</td>
      <td class="numeric-cell">{{ jpim.all_versions_instances_by_state['inactive'] }}</td>
      <td class="numeric-cell">{{ jpim.all_versions_instances_by_state['deleted'] }}</td>
      <td></td>
      <td class="numeric-cell">{{ jpim.all_versions_cores_mcpu_by_state['pending'] / 1000 }}</td>
      <td class="numeric-cell">{{ jpim.all_versions_cores_mcpu_by_state['active'] / 1000 }}</td>
      <td class="numeric-cell">{{ jpim.all_versions_cores_mcpu_by_state['inactive'] / 1000 }}</td>
      <td class="numeric-cell">{{ jpim.all_versions_cores_mcpu_by_state['deleted'] / 1000 }}</td>
    </tr>
  </tbody>
</table>

<h2>Instances</h2>
<table class="data-table" id="instances">
  <thead>
  <tr>
    <th>Name</th>
    <th>Machine Type</th>
    <th>Preemptible</th>
    <th>Location</th>
    <th>Version</th>
    <th>State</th>
    <th>Free Cores</th>
    <th>Failed Requests</th>
    <th>Time Created</th>
    <th>Last Updated</th>
  </tr>
  </thead>
  <tbody>
  {% for instance in instances %}
  <tr>
    <td>{{ instance.name }}</td>
    <td>{{ instance.machine_type }}</td>
    <td>{{ instance.preemptible }}</td>
    <td>{{ instance.location }}</td>
    <td class="numeric-cell">{{ instance.version }}</td>
    <td>{{ instance.state }}</td>
    <td class="numeric-cell">{{ instance.free_cores_mcpu / 1000 }} / {{ instance.cores_mcpu / 1000 }}</td>
    <td class="numeric-cell">{{ instance.failed_request_count }}</td>
    <td>{{ instance.time_created_str() }}</td>
    <td>{{ instance.last_updated_str() }} ago</td>
  </tr>
  {% endfor %}
  </tbody>
</table>
{% endblock %}
